package com.wwwarehouse.contract.release_supply_requirements;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.animation.Animation;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.custom_widget.state_layout.StateLayout;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.contract.adapter.PublishAdapter;
import com.wwwarehouse.contract.bean.SelfPublishBean;
import com.wwwarehouse.contract.common.ContractCommon;
import com.wwwarehouse.contract.core.ContractConstant;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import contract.wwwarehouse.com.contract.R;

/**
 * @创建者 chenchao.
 * @创建时间 2017/9/24 18:09
 * @描述 ${TODO}
 */

public class PublishResourceListFragmentChild extends BaseFragment {


    private View mView;
    private StateLayout mStateLayout;
    private RecyclerView mRecyclerView;
    private Map mCanSeePublishMap;
    private BigDecimal mBigUkid;
    private PublishAdapter mSelfPublishAdapter;
    private Map mDeleteItemMap;
    private List<SelfPublishBean.TempPbResourcesBean> mTempPbResources;
    private int mDelPosition;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_publish_resourse_list_child, container, false);
        return mView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        initData();
    }

    @Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
        return null;
    }

    private void initData() {
        mDeleteItemMap = new HashMap();
        Bundle bundleAsk = getArguments();
        if (bundleAsk != null) {
            int page = bundleAsk.getInt("page");
            long buId = bundleAsk.getLong("buId");
            mCanSeePublishMap = new HashMap();
            mCanSeePublishMap.put("buId", buId);
            mCanSeePublishMap.put("page", page);
            mCanSeePublishMap.put("size", 7);
            mCanSeePublishMap.put("sort", "");
            mCanSeePublishMap.put("type", 0);
            getCanSeePublish();
        }

    }

    private void getCanSeePublish() {
        mStateLayout.showProgressView(true);//加载时候的状态
        NoHttpUtils.httpPost(ContractConstant.QUERY_PUBLISH_LIST, mCanSeePublishMap, mOnResponseListener, 0);
    }

    private NoHttpUtils.OnResponseListener mOnResponseListener = new NoHttpUtils.OnResponseListener() {
        @Override
        public void onSucceed(CommonClass commonClass, int i) {
            switch (i) {
                case 0:
                    if ("0".equals(commonClass.getCode())) {
                        SelfPublishBean selfPublishBean = JSON.parseObject(commonClass.getData().toString(), SelfPublishBean.class);
                        mTempPbResources = selfPublishBean.getTempPbResources();
                        mRecyclerView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                            @Override
                            public void onGlobalLayout() {
                                mRecyclerView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                                int itemHeight = (mRecyclerView.getMeasuredHeight() - mRecyclerView.getPaddingTop() - mRecyclerView.getPaddingBottom()) / 7;
                                mSelfPublishAdapter = new PublishAdapter(mActivity, mTempPbResources, itemHeight);
                                mRecyclerView.setAdapter(mSelfPublishAdapter);
                                mSelfPublishAdapter.setOnDelListener(new PublishAdapter.OnDelListener() {
                                    @Override
                                    public void onDel(TextView button, int position) {
                                        mDelPosition = position;
                                        mDeleteItemMap.put("buId", mTempPbResources.get(position).getBuId());
                                        mDeleteItemMap.put("rsDefinedUkid", mTempPbResources.get(position).getRsDefinedUkid());
                                        NoHttpUtils.httpPost(ContractConstant.REMOVEITEM, mDeleteItemMap, mOnResponseListener, 1);
                                        showProgressDialog();
                                    }
                                });
                            }
                        });

                    } else {
                        mStateLayout.showSystemView(commonClass.getMsg(), true);
                        mStateLayout.setSystemListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                getCanSeePublish();
                            }
                        });
                    }
                    break;
                case 1:
                    if ("0".equals(commonClass.getCode())) {
                        mTempPbResources.remove(mTempPbResources.get(mDelPosition));
                        mSelfPublishAdapter.notifyDataSetChanged();
                        ContractCommon.getInstance().setSeleteToHistory(true);
                    } else {
                        toast(commonClass.getMsg());
                    }
                    break;
            }
        }

        @Override
        public void onFailed(String s, int i) {
            if (i == 0) {
                mStateLayout.showNetworkView(true);
                mStateLayout.setNetworkListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        getCanSeePublish();
                    }
                });
            }
            if (i == 1) {
                dismissProgressDialog();
                toast(s);
            }
        }

        @Override
        public void onFinish(int i) {
            if (i == 0) {
                mStateLayout.showContentView();
            }

            if (i == 1) {
                dismissProgressDialog();
            }
        }
    };

    private void initView() {
        mStateLayout = findView(mView, R.id.sl_state);
        mRecyclerView = findView(mView, R.id.lv_can_see_publish);
        //创建布局管理者
        StaggeredGridLayoutManager staggeredGridLayoutManager;
        staggeredGridLayoutManager = new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(staggeredGridLayoutManager);
    }

    @Override
    public void setTitle() {
//        if (peekFragment() instanceof PublishResourceListFragmentChild)
//        mActivity.setTitle("发布资源清单");
    }

    @Override
    public void requestDatas() {

    }
}
